<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');  

/**
 * Controller for Countries API.
 * @package backend-countries/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Countries_api extends REST_Controller
{
    /*
     * Constructor Method
     */
    public function __construct() {
        parent::__construct();
        
        $this->load->model('country_model');
        $this->load->model('city_model');
        $this->load->model('test2_model');
        
        $this->load->helper('api');
        $this->load->helper('country');
        $this->load->helper('city');
        
        $this->load->library('form_validation');
    }
    
    public function index_get()
    {
        
        $results = $this->test2_model->find(169)->cities()->get();
        
        echo '<pre>';
        print_r($results); 
        echo '</pre>';
        exit();
        
        
//        $params = get_params();
//
//        $countries = $this->country_model->get_api($params['args'], $params['fields'], $params['limit'], $params['offset'], $params['sort'], $params['order']);
//        response_countries($countries);
    }
    
    public function index_post()
    {
        if( valid_add_country() )
        {
            $country = add_country();
            // if successful
            if( $country['id'] )
            {
                format_response('success', 201, 'Add Success', NULL, $country);
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }

    
    public function index_put()
    {
        put_params();
        
        if( valid_edit_country() )
        {
            $country = edit_country();
            // if successful
            if( $country )
            {
                format_response('success', 200, 'Edit Success', NULL, $country);
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }
    
    public function index_delete()
    {
        delete_params();
        
        if( valid_delete_country() )
        {
            $deleted = delete_country();
            if( $deleted === 'deleted' OR $deleted === 'archived' )
            {
                format_response('success', 200, 'Delete Success');
            } else
            {
                format_response('failed', 500, 'Server Error', array('database_error' => 'Database Error'));
            }
        } else
        {
            format_response('failed', 400, 'Invalid Parameter', $this->form_validation->error_array());
        }
    }
    
    public function get_country_by_code_get()
    {
        $params = get_params(4);
        $code = $this->uri->segment(3);
        
        $args = $params['args'];
        $args['code'] = $code;
        
        $countries = $this->country_model->all_api($args, $params['fields'], $params['limit'], $params['offset'], $params['sort'], $params['order'])->get();
        response_countries($countries);
    }
    
    public function get_country_by_id_get()
    {
        $params = get_params(4);

        $id = $this->uri->segment(3);
        
        $args = $params['args'];
        $args['id'] = $id;

        $countries = $this->country_model->all_api($args, $params['fields'], $params['limit'], $params['offset'], $params['sort'], $params['order'])->get();
        response_countries($countries);
    }
    
    public function get_cities_get_by_country_id_get()
    {
        $params = get_params(5);
        $id = $this->uri->segment(3);

        $args = $params['args'];
        
        $country_code = $this->country_model->attribute( array('id' => $id), 'code');
        $args['country_code'] = $country_code;

        $cities = $this->city_model->all_api( $args, '*', $params['limit'], $params['offset'], $params['sort'], $params['order'] )->get();
        response_cities($cities);
    }
    
    public function get_cities_get_by_country_code_get()
    {
        $params = get_params(5);
        $code = $this->uri->segment(3);

        $args = $params['args'];
        $args['country_code'] = $code;

        $cities = $this->city_model->all_api( $args, '*', $params['limit'], $params['offset'], $params['sort'], $params['order'] )->get();
        response_cities($cities);
    }
    
    public function get_cities_post()
    {
//        $params = post_params();
//        $code = $this->uri->segment(3);
//        $cities = $this->city_model->get_all( array('country_code' => $code), '*', $params['limit'], $params['offset'], $params['sort'], $params['order'] );
//        response_cities($cities);
        echo 'cities post';
    }
    
//    public function get_countries_get() {
//        
//    }
//    
//    public function get_countries_post() {
//        
//    }
//    
//    /**
//     * Retrieves all clubs using GET method.
//     * @access public
//     */
//    public function all_get()
//    {
//        $param = retrieve_get_values();
//        $args = array('vips_clubs.status' => 'active');
//        $clubs = $this->club_model->and_justice_for_all($args, '*', $param['limit'], $param['offset'], 'date_created', 'desc');
//        response_clubs($clubs);
//    }
//    
//    /**
//     * Retrieves all clubs using POST method.
//     * @access public
//     */
//    public function all_post()
//    {
//        if(valid())
//        {
//            $param = retrieve_post_values();
//            $args = array('vips_clubs.status' => 'active');
//            $clubs = $this->club_model->and_justice_for_all($args, '*', $param['limit'], $param['offset'], 'date_created', 'desc');
//            response_clubs($clubs);
//        }
//    }
//
//     /**
//     * Retrieves clubs by ID using GET method.
//     * @access public
//     */
//    public function by_id_get()
//    {
//        $club_id = retrieve_get_id();
//        $clubs = $this->club_model->and_justice_for_all(array('vips_clubs.id' => $club_id), '*', 1, 0, 'date_created', 'desc');
//        response_clubs($clubs);
//    }
//    
//    /**
//     * Retrieves clubs by ID using POST method.
//     * @access public
//     */
//    public function by_id_post()
//    {
//        $club_id = retrieve_post_id();
//        $clubs = $this->club_model->and_justice_for_all(array('vips_clubs.id' => $club_id), '*', 1, 0, 'date_created', 'desc');
//        response_clubs($clubs);
//    }
//    
//    /**
//     * Retrieves filtered clubs using GET method.
//     * @access public
//     */
//    public function search_get()
//    {
//        $search_term = retrieve_get_search_term();
//        $param = retrieve_get_values();
//        $clubs = $this->club_model->the_more_i_see(array('title'), $search_term, $this->alias, $param['limit'], $param['offset'], 'date_created', 'desc', $this->tables);
//        response_clubs($clubs);
//    }
//    
//    /**
//     * Retrieves filtered clubs using POST method.
//     * @access public
//     */
//    public function search_post()
//    {
//        if(valid())
//        {
//            $search_term = retrieve_post_search_term();
//            $param = retrieve_post_values();
//            $clubs = $this->club_model->the_more_i_see(array('name'), $search_term, '*', $param['limit'], $param['offset'], 'date_created', 'desc');
//            response_clubs($clubs);
//        }
//    }
    
}

/* End of file countries.php */
/* Location: ./application/modules/backend-countries/controllers/countries.php */